//
//  FragmentViewController.m
//  项目
//
//  Created by 张诗雨 on 15/1/6.
//  Copyright (c) 2015年 张诗雨. All rights reserved.
//

#import "FragmentViewController.h"
#import "AFNetWoringYu.h"
#import "WebColor.h"
#import "FragmentModel.h"
#import "FragmentTableViewCell.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"
#import "MJRefreshFooterView.h"
#import "MJRefreshHeaderView.h"
#import "MJRefresh.h"
#import "OneselfViewController.h"
#import "ChoiceDetailViewController.h"
@interface FragmentViewController ()
@property (nonatomic, assign)NSInteger start;
@property (nonatomic, assign)NSInteger number;
@end

@implementation FragmentViewController
- (void)dealloc
{
    [super dealloc];
    [_tableArray release];
    [_tableView release];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [WebColor whiteSmoke];
    [self requestData];
    [self createTableView];
    [self headerRereshing];
    [self FooterRereshing];
    
}
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[FragmentTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    [_tableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.tableArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FragmentModel *model = [self.tableArray objectAtIndex:indexPath.row];
    FragmentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell.headPic setBackgroundImageWithURL:[model.userinfo objectForKey:@"icon"] forState:UIControlStateNormal];
    [cell.headPic addTarget:self action:@selector(headPicButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.addtimeLable.text = model.addtime_f;
    cell.nameLable.text = [model.userinfo objectForKey:@"uname"];
    cell.shortcontentLable.text = model.shortcontent;
    cell.fromLable.text = @"碎片";
    
    return cell;
}
- (void)headPicButtonAction:(id)sender
{
    FragmentModel *model = [_tableArray objectAtIndex:self.number];
    OneselfViewController *oneselfVC = [[OneselfViewController alloc] init];
    oneselfVC.uid = [model.userinfo objectForKey:@"uid"];
    [self.navigationController pushViewController:oneselfVC animated:YES];
    [oneselfVC release];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.number = indexPath.row;
    FragmentModel *model = [_tableArray objectAtIndex:indexPath.row];
    ChoiceDetailViewController *choiceVC = [[ChoiceDetailViewController alloc] init];
    choiceVC.contentid = model.contentid;
    [choiceVC release];
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FragmentModel *model = [_tableArray objectAtIndex:indexPath.row];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16.0f], NSFontAttributeName, nil];
    CGRect rect = [model.shortcontent boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, 1000) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:dic context:nil];
    CGFloat height = rect.size.height + 170;

    return height;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
// 数据请求
- (void)requestData
{
    [AFNetWoringYu netWorkWithURL: @"http://api.pianke.me/timeline/list" body: @"client=1&limit=10&start=0" resultBlock:^(id result) {
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:result];
        NSArray *arr = [[dic objectForKey:@"data"] objectForKey:@"list"];
        for (NSDictionary *mydic in arr) {
            FragmentModel *model = [[FragmentModel alloc] init];
            [model setValuesForKeysWithDictionary:mydic];
            [self.tableArray addObject:model];
            [model release];
    
        }
        [self.tableView reloadData];
        }];
}
- (void)headerRereshing
{
    // 添加下拉刷新头部控件
    [self.tableView addHeaderWithCallback:^{
        [AFNetWoringYu netWorkWithURL: @"http://api.pianke.me/timeline/list" body: @"client=1&limit=10&start=0" resultBlock:^(id result) {
            NSDictionary *dic = [NSDictionary dictionaryWithDictionary:result];
            NSArray *arr = [[dic objectForKey:@"data"] objectForKey:@"list"];
            for (NSDictionary *mydic in arr) {
                FragmentModel *model = [[FragmentModel alloc] init];
                [model setValuesForKeysWithDictionary:mydic];
                [self.tableArray addObject:model];
                [model release];
                
            }
            [self.tableView reloadData];
            [self.tableView headerEndRefreshing];

        }];
        
    }];
}
- (void)FooterRereshing
{
    self.start = 10;
    [self.tableView addFooterWithCallback:^{
        if (self.start < 100) {
            NSString *body = [NSString stringWithFormat:@"client=1&limit=10&start=%ld", self.start];
            [AFNetWoringYu netWorkWithURL: @"http://api.pianke.me/timeline/list" body:body resultBlock:^(id result) {
                NSDictionary *dic = [NSDictionary dictionaryWithDictionary:result];
                NSArray *arr = [[dic objectForKey:@"data"] objectForKey:@"list"];
                for (NSDictionary *mydic in arr) {
                    FragmentModel *model = [[FragmentModel alloc] init];
                    [model setValuesForKeysWithDictionary:mydic];
                    [self.tableArray addObject:model];
                    [model release];
                    
                }
                [self.tableView reloadData];
                [self.tableView footerEndRefreshing];
                
            }];
            
            self.start+= 10;
        }
    }];

}
@end
